package com.ctl.mq;


import com.alibaba.fastjson.JSON;
import com.ctl.entity.Video;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class VideoConsume {

    private Logger log= LoggerFactory.getLogger(VideoConsume.class);

    @Autowired
    private RestHighLevelClient restHighLevelClient;
    @RabbitListener(queuesToDeclare = @Queue("videos"))
    public void videoConsume(Video video) throws IOException {
        //        创建 es的 索引对象 类型，映射
        IndexRequest indexRequest = new IndexRequest("videos", "video", video.getId() + "");
        //设置文档内容
        indexRequest.source(JSON.toJSONString(video), XContentType.JSON);
        //存入es中
        IndexResponse index = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
        log.debug("录入 es的状态:  "+index.status());
    }

}
